package org.jaaksi.kottlindemo.bus;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.HashMap;
import java.util.LinkedHashMap;

public class BusManager {
  private HashMap<String, BusAction> data = new LinkedHashMap<>();
  private static BusManager instance = new BusManager();

  public static BusManager getInstance() {
    return instance;
  }

  public void register(@NonNull String actionUrl, @NonNull BusAction action) {
    data.put(actionUrl, action);
  }

  public void go(String actionUrl, @Nullable Callback callback) {
    // 从actionUrl中解析出path部分
    BusAction busAction = data.get(actionUrl);
    if (busAction != null) {
      String result = busAction.action(actionUrl);
      if (callback != null) {
        callback.callback(result);
      }
    } else {
       // 丢给路由处理

    }
  }

  public void test() {
    // scheme://savebitmap?data=xxxx
    BusManager.getInstance().register("scheme://savebitmap", new BusAction() {
      @Override
      public String action(String actionUrl) {
        // 解析data数据，保存为图片
        // 返回图片文件地址
        return "";
      }
    });

  }
}
